﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Core;
using MvcEngine.Core.Extensions;
using MvcEngine.Core.Utils;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core.Helpers;
using MvcEngine.Mvc.ModelBinders;
using MvcEngine.Core.Base;

namespace MvcEngine.Blog.ModelBinders
{
    public class BlogModelBinder : ModelBinderBase
    {
        public override object Bind(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;
            BlogData blog = IoC.Resolve<BlogData>();

            blog.AllowComments = request.Form.Get("AllowComments").Contains("true");
            blog.ShowCaptcha = request.Form.Get("ShowCaptcha").Contains("true");
            blog.BlogDescription = request.Form.Get("BlogDescription");
            blog.BlogName = request.Form.Get("BlogName");
            blog.DaysCommentsAreEnabled = request.Form.Get("DaysCommentsAreEnabled").ToInt();
            blog.PostsInRss = request.Form.Get("PostsInRss").ToInt();

            blog.Email = request.Form.Get("Email");
            blog.EmailSubjectPrefix = request.Form.Get("EmailSubjectPrefix");
            blog.PostsPerPage = request.Form.Get("PostsPerPage").ToInt();

            blog.SendMailOnComment = request.Form.Get("SendMailOnComment").Contains("true");
            blog.SmtpPassword = request.Form.Get("SmtpPassword");
            blog.SmtpServer = request.Form.Get("SmtpServer");
            blog.SmtpServerPort = request.Form.Get("SmtpServerPort").ToInt();
            blog.SmtpUserName = request.Form.Get("SmtpUserName");

            blog.HtmlHeader = request.Form.Get("HtmlHeader");

            Validate(blog, controllerContext, new string[] { "Name" });
            return blog;
        }
    }
}
